home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / gmp-132.lha / gmp-1.3.2 / mpz_sqrtrem.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  3KB  |  106 lines

  1. /* mpz_sqrtrem(root,rem,x) -- Set ROOT to floor(sqrt(X)) and REM
  2.    to the remainder, i.e. X - ROOT**2.
  3.  
  4. Copyright (C) 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of the GNU MP Library.
  7.  
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. The GNU MP Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with the GNU MP Library; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "gmp.h"
  23. #include "gmp-impl.h"
  24.  
  25. #ifndef BERKELEY_MP
  26. void
  27. #ifdef __STDC__
  28. mpz_sqrtrem (MP_INT *root, MP_INT *rem, const MP_INT *op)
  29. #else
  30. mpz_sqrtrem (root, rem, op)
  31.      MP_INT *root;
  32.      MP_INT *rem;
  33.      const MP_INT *op;
  34. #endif
  35. #else /* BERKELEY_MP */
  36. void
  37. #ifdef __STDC__
  38. msqrt (const MP_INT *op, MP_INT *root, MP_INT *rem)
  39. #else
  40. msqrt (op, root, rem)
  41.      const MP_INT *op;
  42.      MP_INT *root;
  43.      MP_INT *rem;
  44. #endif
  45. #endif /* BERKELEY_MP */
  46. {
  47.   mp_size op_size, root_size, rem_size;
  48.   mp_ptr root_ptr, op_ptr;
  49.   mp_ptr free_me = NULL;
  50.   mp_size free_me_size;
  51.  
  52.   op_size = op->size;
  53.   if (op_size < 0)
  54.     op_size = 1 / (op_size > 0);    /* Divide by zero for negative OP.  */
  55.  
  56.   if (rem->alloc < op_size)
  57.     _mpz_realloc (rem, op_size);
  58.  
  59.   /* The size of the root is accurate after this simple calculation.  */
  60.   root_size = (op_size + 1) / 2;
  61.  
  62.   root_ptr = root->d;
  63.   op_ptr = op->d;
  64.  
  65.   if (root->alloc < root_size)
  66.     {
  67.       if (root_ptr == op_ptr)
  68.     {
  69.       free_me = root_ptr;
  70.       free_me_size = root->alloc;
  71.     }
  72.       else
  73.     (*_mp_free_func) (root_ptr, root->alloc * BYTES_PER_MP_LIMB);
  74.  
  75.       root->alloc = root_size;
  76.       root_ptr = (mp_ptr) (*_mp_allocate_func) (root_size * BYTES_PER_MP_LIMB);
  77.       root->d = root_ptr;
  78.     }
  79.   else
  80.     {
  81.       /* Make OP not overlap with ROOT.  */
  82.       if (root_ptr == op_ptr)
  83.     {
  84.       /* ROOT and OP are identical.  Allocate temporary space for OP.  */
  85.       op_ptr = (mp_ptr) alloca (op_size * BYTES_PER_MP_LIMB);
  86.       /* Copy to the temporary space.  Hack: Avoid temporary variable
  87.        by using ROOT_PTR.  */
  88.       MPN_COPY (op_ptr, root_ptr, op_size);
  89.     }
  90.     }
  91.  
  92.   rem_size = mpn_sqrt (root_ptr, rem->d, op_ptr, op_size);
  93.  
  94.   root->size = root_size;
  95.  
  96.   /* Write remainder size last, to enable us to define this function to
  97.      give only the square root remainder, if the user calles if with
  98.      ROOT == REM.  */
  99.   rem->size = rem_size;
  100.  
  101.   if (free_me != NULL)
  102.     (*_mp_free_func) (free_me, free_me_size * BYTES_PER_MP_LIMB);
  103.  
  104.   alloca (0);
  105. }
  106.